import { director, game, RenderRoot2D, SpriteFrame } from "cc"
import { DrawManager } from "../Scripts/UI/DrawManager"
import { createUINode } from "../Utils"

export class FadeManager{
    private static instance: FadeManager        
    private _fade: DrawManager

    static getInstance(): FadeManager {
        return this.instance || (this.instance = new FadeManager())
    }

    get fade(): DrawManager {
        if (this._fade){ return this._fade }
        const root = createUINode()
        root.addComponent(RenderRoot2D)

        const node = createUINode()
        node.setParent(root)
        this._fade = node.addComponent(DrawManager)
        this.fade.init()
        // game.addPersistRootNode(root)
        director.addPersistRootNode(root)
        return this._fade

    }

    async fadeIn(duration: number = 200) {
        await this.fade.fadeIn(duration)
    }

    async fadeOut(duration: number = 200) {
        await this.fade.fadeOut(duration)
    }

    async mask() {
        await this.fade.mask()
    }


}